package cn.kuporo.seckill.common.model.response;

import cn.kuporo.seckill.common.exception.BaseExceptionInterface;
import cn.kuporo.seckill.common.exception.SeckillException;
import lombok.Data;

/**
 * @author: kuporo
 * @since: 2025/7/6
 * @description: 响应信息
 */
@Data
public class Response<T> {
    /**
     * 是否成功，默认为 true
     */
    private boolean success = true;
    /**
     * 响应消息
     */
    private String message;

    /**
     * 异常码
     */
    private String errorCode;

    /**
     * 响应数据
     */
    private T data;

    public static <T> Response<T> success() {
        Response<T> response = new Response<>();
        return response;
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }

    public static <T> Response<T> fail() {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }

    public static <T> Response<T> fail(String errorMessage) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(String errorCode, String errorMessage) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(errorCode);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(SeckillException seckillException) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(seckillException.getErrorCode());
        response.setMessage(seckillException.getErrorMessage());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }


}
